// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.skinnedmodel.model.jassimp;

import jassimp.AiBone;
import jassimp.AiBoneWeight;
import jassimp.AiMesh;
import java.util.List;
import zombie.core.skinnedmodel.model.VertexBufferObject;

public final class ImportedSkinnedMesh {
    final ImportedSkeleton skeleton;
    String name;
    VertexBufferObject.VertexArray vertices = null;
    int[] elements = null;

    public ImportedSkinnedMesh(ImportedSkeleton importedSkeleton, AiMesh aiMesh) {
        this.skeleton = importedSkeleton;
        this.processAiScene(aiMesh);
    }

    private void processAiScene(AiMesh aiMesh) {
        this.name = aiMesh.getName();
        int int0 = aiMesh.getNumVertices();
        int int1 = int0 * 4;
        int[] int2 = new int[int1];
        float[] _float = new float[int1];

        for (int int3 = 0; int3 < int1; int3++) {
            _float[int3] = 0.0F;
        }

        List list0 = aiMesh.getBones();
        int int4 = list0.size();

        for (int int5 = 0; int5 < int4; int5++) {
            AiBone aiBone = (AiBone)list0.get(int5);
            String string = aiBone.getName();
            int int6 = (Integer)this.skeleton.boneIndices.get(string);
            List list1 = aiBone.getBoneWeights();

            for (int int7 = 0; int7 < aiBone.getNumWeights(); int7++) {
                AiBoneWeight aiBoneWeight = (AiBoneWeight)list1.get(int7);
                int int8 = aiBoneWeight.getVertexId() * 4;

                for (int int9 = 0; int9 < 4; int9++) {
                    if (_float[int8 + int9] == 0.0F) {
                        _float[int8 + int9] = aiBoneWeight.getWeight();
                        int2[int8 + int9] = int6;
                        break;
                    }
                }
            }
        }

        int int10 = getNumUVs(aiMesh);
        VertexBufferObject.VertexFormat vertexFormat = new VertexBufferObject.VertexFormat(5 + int10);
        vertexFormat.setElement(0, VertexBufferObject.VertexType.VertexArray, 12);
        vertexFormat.setElement(1, VertexBufferObject.VertexType.NormalArray, 12);
        vertexFormat.setElement(2, VertexBufferObject.VertexType.TangentArray, 12);
        vertexFormat.setElement(3, VertexBufferObject.VertexType.BlendWeightArray, 16);
        vertexFormat.setElement(4, VertexBufferObject.VertexType.BlendIndexArray, 16);

        for (int int11 = 0; int11 < int10; int11++) {
            vertexFormat.setElement(5 + int11, VertexBufferObject.VertexType.TextureCoordArray, 8);
        }

        vertexFormat.calculate();
        this.vertices = new VertexBufferObject.VertexArray(vertexFormat, int0);

        for (int int12 = 0; int12 < int0; int12++) {
            this.vertices.setElement(int12, 0, aiMesh.getPositionX(int12), aiMesh.getPositionY(int12), aiMesh.getPositionZ(int12));
            if (aiMesh.hasNormals()) {
                this.vertices.setElement(int12, 1, aiMesh.getNormalX(int12), aiMesh.getNormalY(int12), aiMesh.getNormalZ(int12));
            } else {
                this.vertices.setElement(int12, 1, 0.0F, 1.0F, 0.0F);
            }

            if (aiMesh.hasTangentsAndBitangents()) {
                this.vertices.setElement(int12, 2, aiMesh.getTangentX(int12), aiMesh.getTangentY(int12), aiMesh.getTangentZ(int12));
            } else {
                this.vertices.setElement(int12, 2, 0.0F, 0.0F, 1.0F);
            }

            this.vertices.setElement(int12, 3, _float[int12 * 4], _float[int12 * 4 + 1], _float[int12 * 4 + 2], _float[int12 * 4 + 3]);
            this.vertices.setElement(int12, 4, (float)int2[int12 * 4], (float)int2[int12 * 4 + 1], (float)int2[int12 * 4 + 2], (float)int2[int12 * 4 + 3]);
            if (int10 > 0) {
                int int13 = 0;

                for (int int14 = 0; int14 < 8; int14++) {
                    if (aiMesh.hasTexCoords(int14)) {
                        this.vertices.setElement(int12, 5 + int13, aiMesh.getTexCoordU(int12, int14), 1.0F - aiMesh.getTexCoordV(int12, int14));
                        int13++;
                    }
                }
            }
        }

        int int15 = aiMesh.getNumFaces();
        this.elements = new int[int15 * 3];

        for (int int16 = 0; int16 < int15; int16++) {
            this.elements[int16 * 3 + 2] = aiMesh.getFaceVertex(int16, 0);
            this.elements[int16 * 3 + 1] = aiMesh.getFaceVertex(int16, 1);
            this.elements[int16 * 3 + 0] = aiMesh.getFaceVertex(int16, 2);
        }
    }

    private static int getNumUVs(AiMesh aiMesh) {
        int int0 = 0;

        for (int int1 = 0; int1 < 8; int1++) {
            if (aiMesh.hasTexCoords(int1)) {
                int0++;
            }
        }

        return int0;
    }
}
